﻿package net.WMisiedjan.RedstoneEffects;

import org.bukkit.block.*;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockCanBuildEvent;
import org.bukkit.event.block.BlockListener;
import org.bukkit.event.block.BlockPhysicsEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.block.BlockRedstoneEvent;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.*;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

/**
 * RedstoneEffects block listener
 * 
 * @author SERVER_PreLoaded
 */
public class RedstoneEffectsBlockListener extends BlockListener {
	private final RedstoneEffects plugin;
	public String stuff = "";

	public RedstoneEffectsBlockListener(final RedstoneEffects plugin) {
		this.plugin = plugin;
	}

	@Override
	public void onSignChange(SignChangeEvent event) {
		if (event.getLine(0).equalsIgnoreCase("[effect]")) {
			if (!plugin.permissionsHandler.hasPermission(event.getPlayer(),
					"RedstoneEffects.createsign")) {
				event.getBlock().setType(Material.AIR);
				event.getPlayer()
						.getWorld()
						.dropItemNaturally(event.getBlock().getLocation(),
								new ItemStack(Material.SIGN, 1));
				event.getPlayer()
						.sendMessage(
								"[RedstoneEffects] You don't have the permission to create a sign!");
				return;
			}

			if (event.getLine(1) == null) {
				event.getBlock().setType(Material.AIR);
				event.getPlayer()
						.getWorld()
						.dropItemNaturally(event.getBlock().getLocation(),
								new ItemStack(Material.SIGN, 1));
				event.getPlayer().sendMessage(
						"[RedstoneEffects] No Effect given!");
				return;
			}

			String cname = event.getLine(1);
			try {
				Effect effecty = Effect.valueOf(cname.toUpperCase());

				if (effecty != null) {
					event.getPlayer().sendMessage(
							"[RedstoneEffects] Succesly created effect sign with the effect: "
									+ cname.toUpperCase());
				} else {
					event.getPlayer()
							.sendMessage(
									"[RedstoneEffects] This is not a valid effect. Valid effects are:");

					for (Effect effect : Effect.values()) {
						event.getPlayer().sendMessage(effect.toString());
					}

					event.getBlock().setType(Material.AIR);
					event.getPlayer()
							.getWorld()
							.dropItemNaturally(event.getBlock().getLocation(),
									new ItemStack(Material.SIGN, 1));
					event.getPlayer().sendMessage(
							"[RedstoneEffects] No valid Effect given!");
					return;

				}
			} catch (Exception e) {
				event.getPlayer()
						.sendMessage(
								"[RedstoneEffects] This is not a valid effect. Valid effects are:");
				for (Effect effect : Effect.values()) {
					event.getPlayer().sendMessage(effect.toString());
				}

				event.getBlock().setType(Material.AIR);
				event.getPlayer()
						.getWorld()
						.dropItemNaturally(event.getBlock().getLocation(),
								new ItemStack(Material.SIGN, 1));
				event.getPlayer().sendMessage(
						"[RedstoneEffects] No valid Effect given!");
				return;

			}
		}

	}

	@Override
	public void onBlockRedstoneChange(BlockRedstoneEvent event) {
		if (!(event.getBlock().getState() instanceof Sign)) {
			return;
		}

		Sign signObject = (Sign) event.getBlock().getState();

		// Lets check if the sign is a Transmitter and if the channel name not
		// is empty
		if (!signObject.getLine(0).equalsIgnoreCase("[effect]")) {
			return;
		}

		if ((event.getBlock().isBlockPowered())
				|| (event.getBlock().isBlockIndirectlyPowered())) {
			try
			{
			event.getBlock()
					.getWorld()
					.playEffect(signObject.getBlock().getLocation(),
							Effect.valueOf(signObject.getLine(1).toUpperCase()),
							Integer.parseInt(signObject.getLine(2)));
			}
			catch(Exception e)
			{
				event.getBlock()
				.getWorld()
				.playEffect(signObject.getBlock().getLocation(),
						Effect.valueOf(signObject.getLine(1).toUpperCase()),0);
			}
		}

	}

}
